﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
 
using System.Text;
using System.Windows.Forms;

namespace Demo
{
    public partial class Filtering : DemoBaseUserControl
    {
        FarPoint.Win.Spread.NamedStyle inStyle=new FarPoint.Win.Spread.NamedStyle() ;
        FarPoint.Win.Spread.NamedStyle outStyle=new FarPoint.Win.Spread.NamedStyle() ;
        private string _col2FilterBy="";
        private string _col3FilterBy="";
        public Filtering()
        {
            InitializeComponent();
            this.Title = "过滤";
            this.Description = "Spread .NET  提供过滤功能。你可以使用默认过滤（隐藏行），也可以使用自定义过滤（改变外观）" +
                                 "。本例中通过点击“自定义过滤”和“默认过滤”按钮在“自定义过滤”和“默认过滤”之间切换，点击“取消过滤”按钮取消当前过滤状态。";
        }

        private void Filtering_Load(object sender, EventArgs e)
        {
            this.FpSpread1.DataSource = 销售明细表TableAdapter.GetData();
            for (int i = 0; i < this.FpSpread1_Sheet1.ColumnCount; i++)
            {
                this.FpSpread1_Sheet1.Columns[i].Width = 100;
            }
            FpSpread1.ScrollBarTrackPolicy = FarPoint.Win.Spread.ScrollBarTrackPolicy.Both;

            FpSpread1.Sheets[0].Columns[2].AllowAutoFilter = true;
            FpSpread1.Sheets[0].Columns[3].AllowAutoFilter = true;
            this.rf = FpSpread1.ActiveSheet.RowFilter;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FpSpread1.ActiveSheet.RowFilter.ResetFilter();
        }

        private void fillByToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                //this.销售明细表TableAdapter.FillBy(this.销售数据清单DataSet.销售明细表);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        private void butinStyleColor_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                this.butinStyleColor.BackColor = this.colorDialog1.Color;
                inStyle.BackColor = this.colorDialog1.Color;
            }
        }

        private void butoutStyleColor_Click(object sender, EventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                this.butoutStyleColor.BackColor = this.colorDialog1.Color;
                outStyle.BackColor = this.colorDialog1.Color;
            }
        }

        private void butSure_Click(object sender, EventArgs e)
        {

                 _col2FilterBy = FpSpread1.ActiveSheet.RowFilter.GetColumnFilterBy(2);
                 _col3FilterBy = FpSpread1.ActiveSheet.RowFilter.GetColumnFilterBy(3);

                FarPoint.Win.Spread.FilterColumnDefinition fcd1 = new FarPoint.Win.Spread.FilterColumnDefinition(2);
                FarPoint.Win.Spread.FilterColumnDefinition fcd2 = new FarPoint.Win.Spread.FilterColumnDefinition(3);

                inStyle.BackColor = butinStyleColor.BackColor;
                outStyle.BackColor = butoutStyleColor.BackColor;

                FarPoint.Win.Spread.StyleRowFilter rowFilter = new FarPoint.Win.Spread.StyleRowFilter(FpSpread1.ActiveSheet, inStyle, outStyle);

                rowFilter.AddColumn(fcd1);
                rowFilter.AddColumn(fcd2);

                FpSpread1.ActiveSheet.RowFilter = rowFilter;
                if (_col2FilterBy != null && _col2FilterBy.Length != 0)
                    FpSpread1.ActiveSheet.AutoFilterColumn(2, _col2FilterBy, 0);
                if (_col3FilterBy != null && _col3FilterBy.Length != 0)
                    FpSpread1.ActiveSheet.AutoFilterColumn(3, _col3FilterBy, 0);        
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string col2FilterBy = FpSpread1.ActiveSheet.RowFilter.GetColumnFilterBy(2);
            string col3FilterBy = FpSpread1.ActiveSheet.RowFilter.GetColumnFilterBy(3);

            FpSpread1.ActiveSheet.RowFilter = rf;

            if (col2FilterBy != null && col2FilterBy.Length != 0)
                FpSpread1.ActiveSheet.AutoFilterColumn(2, col2FilterBy, 0);
            if (col3FilterBy != null && col3FilterBy.Length != 0)
                FpSpread1.ActiveSheet.AutoFilterColumn(3, col3FilterBy, 0);
        }
    }
}